package com.liuqi.aiagent.service.impl;

import com.liuqi.aiagent.mapper.VariableMapper;
import com.liuqi.aiagent.service.VariableService;
import com.liuqi.aiagent.transfer.ApiResult;
import com.liuqi.aiagent.transfer.dto.VariableEditDto;
import com.liuqi.aiagent.transfer.dto.VariableSaveDto;
import com.liuqi.aiagent.transfer.entity.Variable;
import com.liuqi.aiagent.transfer.mapstruct.VariableMapStruct;
import com.liuqi.aiagent.transfer.model.Result;
import com.liuqi.aiagent.transfer.model.ResultList;
import com.liuqi.aiagent.transfer.vo.VariableVo;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liuqi
 * @date 2025/7/15
 **/
@Service
@Slf4j
public class VariableServiceImpl implements VariableService {

    private final List<VariableVo> variableCaches;

    private final VariableMapper variableMapper;

    @Autowired
    public VariableServiceImpl(VariableMapper variableMapper) {
        this.variableMapper = variableMapper;
        List<VariableVo> dataList = findAll0();
        this.variableCaches = dataList == null ? new ArrayList<>(1) : dataList;
        log.info("查询系统变量: " + variableCaches);
    }

    @Override
    public List<VariableVo> getVariables() {
        if (variableCaches != null) {
            return new ArrayList<>(variableCaches);
        } else {
            return new ArrayList<>(0);
        }
    }

    @Override
    public ResultList<VariableVo> findAll() {
        if (variableCaches != null) {
            return ApiResult.success(variableCaches, variableCaches.size());
        } else {
            List<VariableVo> variableVos = findAll0();
            return ApiResult.success(variableVos, variableVos.size());
        }
    }

    @Override
    public Result save(VariableSaveDto variableSaveDto) {
        if (variableMapper.findByName(variableSaveDto.getName()) != null) {
            return ApiResult.fail("变量名称已存在");
        }
        log.info("添加变量: 变量名称[{}], 变量值[{}]", variableSaveDto.getName(), variableSaveDto.getValue());
        variableMapper.save(VariableMapStruct.INSTANCE.from(variableSaveDto));
        resetCache();
        return ApiResult.success();
    }

    @Override
    public Result edit(VariableEditDto variableEditDto) {
        log.info("编辑变量: 变量名称[{}], 变量值[{}]", variableEditDto.getName(), variableEditDto.getValue());
        variableMapper.edit(VariableMapStruct.INSTANCE.from(variableEditDto));
        resetCache();
        return ApiResult.success();
    }

    @Override
    public Result delete(int id) {
        log.warn("删除变量, 变量id[{}]", id);
        variableMapper.delete(id);
        resetCache();
        return ApiResult.success();
    }

    private List<VariableVo> findAll0() {
        List<Variable> variables = variableMapper.findAll();
        List<VariableVo> variableVos = VariableMapStruct.INSTANCE.toVos(variables);
        return variableVos;
    }

    private synchronized void resetCache() {
        // 清空缓存
        variableCaches.clear();
        // 加载缓存
        List<VariableVo> dataList = findAll0();
        variableCaches.addAll(dataList);
    }

}
